package soccerBet.project.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import soccerBet.project.objects.*;
import soccerBet.project.utils.*;

public class PrecosDAO {
	

	
	
	public void guardarPrecos(Precos preco){
	       
		
		 Session session = HibernateUtil.getSessionFactory().openSession();
		 Transaction tx = null;
		 try {
		     tx = session.beginTransaction();

		     session.save(preco);
		     
		     tx.commit();
		 }
		 catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     try {
				throw e;
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				System.out.println("commit preice: "+e1.getStackTrace().toString());
			}
		 }
		 finally {
			 session.close();
		 }
		
	 
		
	 }
	
	 public Precos gePreco(Integer idPrecos) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		
	
		
		Precos preco = (Precos)  session.
		createQuery("select p from Precos as p where p.idPrecos = ?").setInteger(0, idPrecos).uniqueResult();
		
		
		
		session.close();
		
		return preco;

	 }



	public void guardarPrecosRunner(Precos preco) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();

		session.saveOrUpdate(preco);
		session.saveOrUpdate(preco.getRunner());

		// Commit the transaction
		session.getTransaction().commit();
		
		
		session.close();
	}

	public Apostas getAposta(Integer idPrecos) {
		// TODO Auto-generated method stub
		try{
			
		Session session = HibernateUtil.getSessionFactory().openSession();

//		Apostas aposta = (Apostas)  session.
//		createQuery("select a from Apostas as a join a.runners as r join r.precoses as p where p.idPrecos = ?").setInteger(0, idPrecos).uniqueResult();
//
//		if(aposta==null){
//			System.out.println("teste3");
//			
//			aposta = (Apostas)  session.
//			createQuery("select a from Apostas as a join a.runners as r join r.precoses as p where p.idPrecos = ?").setInteger(0, idPrecos).uniqueResult();
//
//			
//		}
		
		 Precos p = (Precos)  (session.load(Precos.class,idPrecos));
		 
		 if(p==null){
			 System.out.println("FAILLLLLLLLLLLLLLLLLL"+idPrecos);
		 }
		 
		 Runner runner = (Runner)  (session.get(Runner.class,p.getRunner().getIdRunner()));
		 
		 Apostas aposta = (Apostas) (session.get(Apostas.class,runner.getApostas().getIdAposta()));
		
		
		 session.close();
		
	
		
		return aposta;
		
		}catch(Exception e){
			System.out.println("Excepcao5:"+e.getMessage());
			return null;
		}
		
		
		
	}

	public Runner getRunner(Integer idPrecos) {
		// TODO Auto-generated method stub
		
		try{

		Session session = HibernateUtil.getSessionFactory().openSession();
		
		
//		Runner runner = (Runner)  session.
//		createQuery("select r from Runner as r join r.precoses as p where p.idPrecos = ?").setInteger(0, idPrecos).uniqueResult();
//
//		if(runner==null){


		 Precos p = (Precos)  (session.get(Precos.class,idPrecos));
		 
		 if(p==null){
			 System.out.println("FAILLLLLLLLLLLLLLLLLL");
		 }
		 
		 Runner runner = (Runner)  (session.get(Runner.class,p.getRunner().getIdRunner()));

//		
//		}

		session.close();
		
		
		
		
		return runner;
		
		}catch(Exception e){
			System.out.println("Excepcao5:"+e.getMessage());
			return null;
		}
		
		
	}
	

	    
	}